package service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dao.AccountDAO;
import dao.BusinessDAO;
import dao.GoodsDAO;
import dao.GoodsPicsDAO;
import dao.OrderDAO;
import dao.OrderGoodsDAO;
import po.Account;
import po.Business;
import po.Goods;
import po.GoodsPics;
import po.Order;
import po.OrderGoods;
import service.IBusiness;
@Service("ib")
public class IBusinessImpl implements IBusiness {
	@Autowired(required=true)
	private OrderDAO od;
	@Autowired(required=true)
	private GoodsDAO gd;
	@Autowired(required=true)
	private GoodsPicsDAO gpd;
	@Autowired(required=true)
	private OrderGoodsDAO ogd;
	@Autowired(required=true)
	private AccountDAO ad;
	
	
	@Transactional
	@Override
	public int pullOff(String[] ids) {
		// TODO Auto-generated method stub
		if(ids==null){
			return -1;
		}
		for(int i=0;i<ids.length;i++){
			gd.delGoods(ids[i]);
		}
		
		return 0;
	}

	@Override
	public int accept(String orderID) {
		// TODO Auto-generated method stub
		od.updateOrderStatus(orderID, 0);
		return 0;
	}

	@Override
	public int reject(String orderID) {
		// TODO Auto-generated method stub
		od.updateOrderStatus(orderID, 1);
		return 0;
	}

	@Override
	public int deliver(String orderID) {
		// TODO Auto-generated method stub
		od.updateOrderStatus(orderID, 2);
		return 0;
	}

	@Override
	public List<Order> queryOrders(String bid) {
		// TODO Auto-generated method stub
		System.out.println("queryOrders from db bid");
		List<Order> myOrder = od.listOrdersByBid(bid);
		
		for(int i=0;i<myOrder.size();i++){
			List<OrderGoods> lsog = ogd.findOrder(myOrder.get(i).getOid());
			Map<String, Integer> goodsIDs = new HashMap<>();
			for(int j=0;j<lsog.size();j++)
				goodsIDs.put(lsog.get(j).getGid(), lsog.get(j).getNum());
			myOrder.get(i).setGoodsIDs(goodsIDs );
		}
		return myOrder;
	}
	
	@Transactional
	@Override
	public int putOn(Goods goods) {
		// TODO Auto-generated method stub
		
		List<String> lsPicPath = goods.getPicPath();
		
		GoodsPics gp=null;
		for(int i=0;i<lsPicPath.size();i++){
			gp=new GoodsPics();
			//gp.setId("111");
			gp.setGid(goods.getId());
			gp.setPicPath(lsPicPath.get(i));
			System.out.println(gp.getPicPath()+gp.getGid()+gp.getId());
			gpd.addGoodsPics(gp);
		}
		gd.addGoods(goods);
		return 0;
	}

	@Override
	public int updateGoods(Goods goods) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Goods> queryGoods(String bid) {
		// TODO Auto-generated method stub
		
		return gd.listGoodsByBid(bid);
	}

	@Override
	public Account loginIn(Account acc) {
		// TODO Auto-generated method stub
		
		return ad.getAccountId(acc.getAccount(), acc.getPsd(), acc.getIdentity());
	}

}
